package com.bootdo.provider.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.provider.domain.ProviderInfoDO;
import com.bootdo.provider.service.ProviderInfoService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 供应商表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-04-20 11:24:50
 */
 
@Controller
@RequestMapping("/provider/providerInfo")
public class ProviderInfoController {
	@Autowired
	private ProviderInfoService providerInfoService;
	
	@GetMapping()
	@RequiresPermissions("provider:providerInfo:providerInfo")
	String ProviderInfo(){
	    return "provider/providerInfo/providerInfo";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("provider:providerInfo:providerInfo")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ProviderInfoDO> providerInfoList = providerInfoService.list(query);
		int total = providerInfoService.count(query);
		PageUtils pageUtils = new PageUtils(providerInfoList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "provider/providerInfo/add";
	}

	@GetMapping("/edit/{providerId}")
	String edit(@PathVariable("providerId") Long providerId,Model model){
		ProviderInfoDO providerInfo = providerInfoService.get(providerId);
		model.addAttribute("providerInfo", providerInfo);
	    return "provider/providerInfo/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( ProviderInfoDO providerInfo){
		if(providerInfoService.save(providerInfo)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( ProviderInfoDO providerInfo){
		providerInfoService.update(providerInfo);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long providerId){
		if(providerInfoService.remove(providerId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] providerIds){
		providerInfoService.batchRemove(providerIds);
		return R.ok();
	}


	/**
	 * 查询所有供应商
	 */
	@ResponseBody
	@GetMapping("/all/list")
    public List<ProviderInfoDO> allList(@RequestParam(required = false) String providerSname){
		Map<String,Object> map = new HashMap<>();
		map.put("providerSname",providerSname);
//		Query query = new Query(map);
		List<ProviderInfoDO> providerInfoList = providerInfoService.list(map);
		return providerInfoList;
	}


	/**
	 * 根据供应商id查询供应商
	 */
	@GetMapping("providerById")
	@ResponseBody
	public String providerById(@RequestParam Long providerId){
		return providerInfoService.get(providerId).getProviderFname();
	}

}
